package org.activision.net.packet;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activision.Constants;
import org.activision.Engine;
import org.activision.io.InStream;
import org.activision.io.impl.RSObjectsRegion;
import org.activision.io.scripts.Scripts;
import org.activision.io.scripts.interfaceScript;
import org.activision.io.scripts.objectScript;
import org.activision.model.Item;
import org.activision.model.World;
import org.activision.model.npc.Npc;
import org.activision.model.player.ChatMessage;
import org.activision.model.player.Inventory;
import org.activision.model.player.Player;
import org.activision.model.player.Skills;
import org.activision.net.codec.ConnectionHandler;
import org.activision.task.Task;
import org.activision.util.Misc;
import org.activision.util.ProjectileManager;
import org.activision.util.RSTile;

public class Packets {

	private static final Map<Short, Method> packets = new HashMap<Short, Method>();
	private static final byte[] PacketSize = new byte[256];

	private static void reset() {
		for (int i = 0; i < 256; i++) {
			PacketSize[i] = -3;
		}
		PacketSize[0] = 8;
		PacketSize[1] = -1;
		PacketSize[2] = -1;
		PacketSize[3] = -1;
		PacketSize[4] = 1;
		PacketSize[5] = 4;
		PacketSize[6] = 0;
		PacketSize[7] = 6;
		PacketSize[8] = -1;
		PacketSize[9] = 7;
		PacketSize[10] = 16;
		PacketSize[11] = 3;
		PacketSize[12] = -1;
		PacketSize[13] = 8;
		PacketSize[14] = 8;
		PacketSize[15] = 3;
		PacketSize[16] = 7;
		PacketSize[17] = 4;
		PacketSize[18] = 2;
		PacketSize[19] = 7;
		PacketSize[20] = 8;
		PacketSize[21] = 3;
		PacketSize[22] = 2;
		PacketSize[23] = 15;
		PacketSize[24] = 8;
		PacketSize[25] = -1;
		PacketSize[26] = 3;
		PacketSize[27] = -1;
		PacketSize[28] = 11;
		PacketSize[29] = 7;
		PacketSize[30] = 0;
		PacketSize[31] = 2;
		PacketSize[32] = 2;
		PacketSize[33] = 7;
		PacketSize[34] = 6;
		PacketSize[35] = 4;
		PacketSize[36] = 3;
		PacketSize[37] = -1;
		PacketSize[38] = 15;
		PacketSize[39] = 0;
		PacketSize[40] = 8;
		PacketSize[41] = 4;
		PacketSize[42] = 3;
		PacketSize[43] = 7;
		PacketSize[44] = 4;
		PacketSize[45] = 2;
		PacketSize[46] = -1;
		PacketSize[47] = -1;
		PacketSize[48] = 8;
		PacketSize[49] = 4;
		PacketSize[50] = 3;
		PacketSize[51] = -1;
		PacketSize[52] = 8;
		PacketSize[53] = -1;
		PacketSize[54] = -1;
		PacketSize[55] = 8;
		PacketSize[56] = 7;
		PacketSize[57] = 11;
		PacketSize[58] = 12;
		PacketSize[59] = 3;
		PacketSize[60] = -1;
		PacketSize[61] = 4;
		PacketSize[62] = -1;
		PacketSize[63] = 3;
		PacketSize[64] = 7;
		PacketSize[65] = 3;
		PacketSize[66] = 3;
		PacketSize[67] = -1;
		PacketSize[68] = 2;
		PacketSize[69] = -1;
		PacketSize[70] = 3;
		PacketSize[71] = -1;
		PacketSize[72] = -1;
		PacketSize[73] = 6;
		PacketSize[74] = 3;
		PacketSize[75] = -1;
		PacketSize[76] = 3;
		PacketSize[77] = -1;
		PacketSize[78] = 7;
		PacketSize[79] = 8;
		PacketSize[80] = 7;
		PacketSize[81] = -1;
		PacketSize[82] = 16;
		PacketSize[83] = 1;
	}

	public Packets() {
		final List<Short> Packets = new ArrayList<Short>(255);
		Packets.add((short) 0);
		Packets.add((short) 3);
		Packets.add((short) 6);
		Packets.add((short) 10);
		Packets.add((short) 13);
		Packets.add((short) 14);
		Packets.add((short) 19);
		Packets.add((short) 20);
		Packets.add((short) 24);
		Packets.add((short) 25);
		Packets.add((short) 29);
		Packets.add((short) 34);
		Packets.add((short) 37);
		Packets.add((short) 39);
		Packets.add((short) 40);
		Packets.add((short) 48);
		Packets.add((short) 52);
		Packets.add((short) 53);
		Packets.add((short) 55);
		Packets.add((short) 60);
		Packets.add((short) 66);
		Packets.add((short) 71);
		Packets.add((short) 73);
		Packets.add((short) 78);
		Packets.add((short) 79);
		Packets.add((short) 82);
		Packets.add((short) 83);
		Short[] PacketsA = new Short[Packets.size()];
		Packets.toArray(PacketsA);
		reset();
		setPackets(PacketsA);
	}

	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_0(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 0, buttonId, buttonId2, buttonId3);
	}

	/*
	 * Mouse
	 */
	@SuppressWarnings("unused")
	private static void PacketId_3(InStream Packet, int Size, Player p) {
	}

	@SuppressWarnings("unused")
	private static void PacketId_6(InStream Packet, int Size, Player p) {
		if (p.getIntermanager().containsTab(16))
			p.getFrames().closeInterface(16);
	}

	/*
	 * Switch
	 */
	@SuppressWarnings("unused")
	private static void PacketId_10(InStream Packet, int Size, Player p) {
		int fromInterfaceHash = Packet.readInt();
		int fromInterfaceId = fromInterfaceHash >> 16;
		int junk = Packet.readShort();
		int toSlot = Packet.readShort128() - 28;
		int toInterfaceHash = Packet.readIntLE();
		int toInterfaceId = toInterfaceHash >> 16;
		int junk1 = Packet.readShortLE128();
		int fromSlot = Packet.readShortLE();
		switch (fromInterfaceId) {
		case 149:
			switch (toInterfaceId) {
			case 149:
				if (fromSlot < 0
						|| fromSlot >= Inventory.SIZE
						|| p.getInventory().getContainer().get(fromSlot) == null)
					return;
				if (toSlot < 0 || fromSlot >= Inventory.SIZE)
					return;
				Item toSlotItem = p.getInventory().getContainer().get(toSlot);
				p.getInventory().getContainer().set(toSlot,
						p.getInventory().getContainer().get(fromSlot));
				p.getInventory().getContainer().set(fromSlot, toSlotItem);
				break;
			}
			break;
		}
	}

	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_13(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 13, buttonId, buttonId2, buttonId3);
	}

	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_14(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 0, buttonId, buttonId2, buttonId3);
	}
	/*
	 * Object option1
	 */
	@SuppressWarnings("unused")
	private static void PacketId_19(InStream Packet, int Size, Player p) {
		int coordY = Packet.readUnsignedShortLE128();
		int coordX = Packet.readUnsignedShortLE128();
		int objectId = Packet.readUnsignedShortLE();
		int height = Packet.readUnsignedByteC();
		RSTile location = RSTile.createRSTile(coordX, coordY, height);
		if(!p.getMask().getRegion().isUsingStaticRegion() && !RSObjectsRegion.objectExistsAt(objectId, location))
			return;
		if(!p.getLocation().withinDistance(location, 1)) {
			return;
		}
		objectScript object = Scripts.invokeObjectScript(objectId);
		if(object == null) {
			System.out.println("Unhandled Object: " + objectId);
			return;
		}
		object.option1(p, coordX, coordY, height);
	}
	
	/*
	 * Object option1
	 */
	@SuppressWarnings("unused")
	private static void PacketId_78(InStream Packet, int Size, Player p) {
		int coordY = Packet.readUnsignedShort();
		int objectId = Packet.readUnsignedShortLE128();
		int coordX = Packet.readUnsignedShortLE128();
		int height = Packet.readUnsignedByteC();
		p.getConstruction().clickObject(objectId, coordX, coordY, height);
	}
	
	
	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_20(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 20, buttonId, buttonId2, buttonId3);
	}

	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_24(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 24, buttonId, buttonId2, buttonId3);
	}

	/*
	 * Chat
	 */
	@SuppressWarnings("unused")
	private static void PacketId_25(InStream Packet, int Size, Player p) {
		int effects = Packet.readUnsignedShort();
		int numChars = Packet.readUnsignedByte();
		if(numChars > 100 || numChars < 0 || effects < 0)
			return;
		String text = Misc.decryptPlayerChat(Packet, numChars);
		if(text == null || text.equals(""))
			return;
		StringBuilder newText = new StringBuilder();
		boolean wasSpace = true;
		for (int i = 0; i < text.length(); i++) {
			if (wasSpace) {
				newText.append(("" + text.charAt(i)).toUpperCase());
				if (!String.valueOf(text.charAt(i)).equals(" "))
					wasSpace = false;
			} else
				newText.append(("" + text.charAt(i)).toLowerCase());
			if (String.valueOf(text.charAt(i)).contains(".")
					|| String.valueOf(text.charAt(i)).contains("!")
					|| String.valueOf(text.charAt(i)).contains("?"))
				wasSpace = true;
		}
		text = newText.toString();
		p.getMask()
				.setLastChatMessage(new ChatMessage(effects, numChars, text));
		p.getMask().setChatUpdate(true);
	}

	/*
	 * pickupItem
	 */
	@SuppressWarnings("unused")
	private static void PacketId_29(InStream Packet, int Size, Player p) {
		int z = Packet.readByteC();
		int x = Packet.readShort128();
		int id = Packet.readShortLE();
		int y = Packet.readShortLE128();
		if (p.getInventory().hasRoomFor(id, 1)) {
			p.getInventory().addItem(id, 1);
		}
	}

	/*
	 * Click
	 */
	@SuppressWarnings("unused")
	private static void PacketId_34(InStream Packet, int Size, Player p) {
		Packet.readShort();
		Packet.readInt();
	}

	/*
	 * Chat
	 */
	@SuppressWarnings("unused")
	private static void PacketId_37(InStream Packet, int Size, Player p) {
	}

	/*
	 * Ping
	 */
	@SuppressWarnings("unused")
	private static void PacketId_39(InStream Packet, int Size, Player p) {
	}

	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_40(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 40, buttonId, buttonId2, buttonId3);
	}

	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_48(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 48, buttonId, buttonId2, buttonId3);
	}

	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_52(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 52, buttonId, buttonId2, buttonId3);
	}

	/*
	 * Commands
	 */
	@SuppressWarnings("unused")
	private static void PacketId_53(InStream Packet, int Size, final Player p) {
		boolean bool = Packet.readUnsignedByte() == 1;
		String command = Packet.readJagString().toLowerCase();
		String[] cmd = command.split(" ");
		if (command.equals("rpacket")) {
			packets.clear();
			new Packets();
		}else if (command.equals("hint")) {
			p.getHinticonmanager().addHintIcon(World.getPlayers().get(1), 0, 40497, false);
			//p.getHinticonmanager().removeHintIcon(0);
			//p.getHinticonmanager().removeUnsavedIcon();
			//p.getHinticonmanager().addHintIcon(p.getLocation().getX(), p.getLocation().getY(), 1, 50, 4, 2, 40497, true);
		}else if (cmd[0].equals("pnpc")) {
			p.getAppearence().setNpcType((short) Integer.parseInt(cmd[1]));
			p.getMask().setApperanceUpdate(true);
		}else if (command.equals("teletest")) {
			p.getCombatDefinitions().doEmote(8939, 2617, 1800);
			Engine.getEntityExecutor().schedule(new Task() {
				@Override
				public void run() {
					p.getCombatDefinitions().doEmote(8941, 2618, 2400);
					p.getMask().getRegion().teleport(3222, 3222, 0, 0);
				}
			}, 1801);
		}else if (command.equals("zombieattack")) {
			for (Player player : World.getPlayers()) {
				if (player == null)
					continue;
				synchronized (player) {
					if(!player.isOnline())
						continue;
					player.graphics(2378);
					player.animate(2836);
					int message = Misc.random(2);
					if(message == 0)
						player.getMask().setLastChatMessage(new ChatMessage(0, 10, "Help!!!"));
					else if(message == 1)
						player.getMask().setLastChatMessage(new ChatMessage(0, 20, "Oah! Its a zombie!"));
					else if(message == 2)
						player.getMask().setLastChatMessage(new ChatMessage(0, 20, "Where is my mother =(?"));
					player.getMask().setChatUpdate(true);
				}
			}
		}else if (command.equals("testt")) {
			for(int i = 0; i < 250; i++) {
				/*ConnectionHandler connection = new ConnectionHandler(null);
				connection.setPlayer(new Player("bot"+i, "lol", Calendar.getInstance(), Calendar.getInstance(),(short) 0, "",(byte)0));
				connection.getPlayer().getLocation().set(connection.getPlayer().getLocation().getX()+Misc.random(10), connection.getPlayer().getLocation().getY()+Misc.random(10));
				World.registerConnection(connection);*/
				World.getNpcs().add(new Npc((short) i, p.getLocation()));
			}
		}else if (command.equals("playerson")) {
			System.out.println("Players  on: "+World.getPlayers().size());
		}else if (command.equals("killall")) {
			for (Player player : World.getPlayers()) {
				if (player == p || player == null)
					continue;
				synchronized (player) {
					if(!player.isOnline())
						continue;
					player.graphics(2315);
					player.getMask().setLastChatMessage(new ChatMessage(0, 10, "What the f***?"));
					player.getMask().setChatUpdate(true);
				}
			}
			Engine.getEntityExecutor().schedule(new Task() {
				@Override
				public void run() {
					for (Player player : World.getPlayers()) {
						if (player == p || player == null)
							continue;
						synchronized (player) {
							if(!player.isOnline())
								continue;
							player.hit(player.getSkills().getHitPoints());
							player.getMask().setLastChatMessage(new ChatMessage(0, 10, "Oh my godness!"));
							player.getMask().setChatUpdate(true);
						}
					}
				}
			}, 600);
		} else if (command.equals("food")) {
				p.getInventory().addItem(385, 28);
		} else if (cmd[0].equals("spell")) {
			ProjectileManager.sendGlobalProjectile(p,
					World.getPlayers().get(2), 2263, 11, 11, 30, 20, 0);
		} else if (command.equals("test")) {
			p.getFrames().sendGroundItem(p.getLocation(), new Item(11694),
					false);
		} else if (command.equals("coords")) {
			p.getFrames().sendChatMessage(Constants.COMMANDS_MESSAGE,
					"Your position is: " + p.getLocation().toString());
		} else if (cmd[0].equals("inter")) {
			p.getFrames().sendInterface(Integer.parseInt(cmd[1]));
		} else if (cmd[0].equals("tele")) {
			p.getMask().getRegion().teleport(Integer.parseInt(cmd[1]),
					Integer.parseInt(cmd[2]), Integer.parseInt(cmd[3]), 0);
		} else if (cmd[0].equals("teleh")) {
			p.getMask().getRegion().teleport(Integer.parseInt(cmd[1]),
					Integer.parseInt(cmd[2]), Integer.parseInt(cmd[3]), 0);
		} else if (cmd[0].equals("item")) {
            if(cmd.length == 2 || cmd.length == 3) {
                    int item = Integer.valueOf(cmd[1]);
                    int amount = 1;
                if(cmd.length == 3) {
                        amount = Integer.valueOf(cmd[2]);
                        p.getInventory().addItem(item, amount);
            }
            }
		} else if (cmd[0].equals("pray")) {
			p.getPrayer().switchPrayBook(Boolean.parseBoolean(cmd[1]));
		} else if (cmd[0].equals("dialogue")) {
			p.getDialogue().startDialogue("Npc_default");
		} else if (cmd[0].equals("gfx")) {
			p.graphics(Integer.parseInt(cmd[1]));
		} else if (cmd[0].equals("gfx2")) {
			p.graphics2(Integer.parseInt(cmd[1]));
		} else if (cmd[0].equals("emote")) {
			p.animate(Integer.parseInt(cmd[1]));
		} else if (cmd[0].equals("bank")) {
			p.getBank().openBank();
		} else if (cmd[0].equals("npctest")) {
			World.getNpcs().get(1).animate(Integer.parseInt(cmd[1]));
		} else if (cmd[0].equals("hp")) {
			p.getSkills().heal(990);
		} else if (cmd[0].equals("poh")) {
			p.getConstruction().loadHouse();
		} else if (command.equals("spec")) {
			p.getCombatDefinitions().setSpecpercentage((byte) 100);
			p.getCombatDefinitions().refreshSpecial();
		} else if (command.equals("prayer")) {
			p.getSkills().set(Skills.PRAYER, 99);
		} else if (cmd[0].equals("config")) {
			p.getFrames().sendConfig(Integer.parseInt(cmd[1]),
					Integer.parseInt(cmd[2]));
		} else if (cmd[0].equals("string")) {
			p.getFrames().sendString(cmd[1], Integer.parseInt(cmd[2]),
					Integer.parseInt(cmd[3]));
		} else if (command.equals("master")) {
			for (int i = 0; i < 25; i++) {
				p.getSkills().addXp(i, Skills.MAXIMUM_EXP);
			}
		} else if (command.equals("masteradmin")) {
			for (int i = 0; i < 25; i++) {
				p.getSkills().set(i, 255);
				p.getSkills().heal(p.getSkills().getLevel(3)*10);
			}
		} else if (command.equals("reset")) {
			for (int i = 0; i < 25; i++) {
				p.getSkills().set(i, 1);
				p.getSkills().setXp(i, 0);
			}
		} else if (command.equals("hit")) {
			//p.getSkills().heal(1000);
			p.hit(500);
			p.hit(489);
		} else if (cmd[0].equals("hit3")) {
			p.heal(Integer.parseInt(cmd[1]), Integer.parseInt(cmd[2]), Integer
					.parseInt(cmd[3]));
		} else if (cmd[0].equals("npc")) {
			World.getNpcs().add(new Npc((short) Integer.parseInt(cmd[1]), p.getLocation()));
		} else if (cmd[0].equals("buy")) {
			p.getFrames().sendConfig(1109, -1);
			p.getFrames().sendConfig(1110, 0);
			p.getFrames().sendConfig(1111, 0);
			p.getFrames().sendConfig(1112, 0);
			p.getFrames().sendConfig(1113, 0);
			p.getFrames().sendConfig(1114, 0);
			p.getFrames().sendConfig(1115, 0);
			p.getFrames().sendConfig(1116, 0);
			p.getFrames().sendInterface(105);
			p.getFrames().sendInventoryInterface(449);
			p.getFrames().sendAMask(0, 21, 449, -1, -1);
			p.getFrames().sendInterface(7, 752, 7, 389);
			Object[] o = new Object[] { "Grand Exchange Item Search" };
			p.getFrames().sendClientScript(570, o, "s");
		} else if (cmd[0].equals("ge")) {
			p.getFrames().sendGE();
		} else if (cmd[0].equals("fletch")) {
			p.getFrames().testFletch();
		}

	}

	/*
	 * ActionButtom
	 */
	@SuppressWarnings("unused")
	private static void PacketId_55(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 55, buttonId, buttonId2, buttonId3);
	}

	/*
	 * Click player
	 */

	@SuppressWarnings("unused")
	private static void PacketId_66(InStream Packet, int Size, Player p) {
		int playerIndex = Packet.readShort128();
		Packet.readByte128();
		if (playerIndex < 0 || playerIndex >= Constants.MAX_AMT_OF_PLAYERS)
			return;
		Player p2 = World.getPlayers().get(playerIndex);
		if (p2 == null || !p2.isOnline())
			return;
		p.getCombat().attack(p2);
	}

	/*
	 * Walking
	 */
	@SuppressWarnings("unused")
	private static void PacketId_60(InStream Packet, int Size, Player p) {
		int steps = (Size - 5) / 2;
		if(steps > 25)
			return;
		int firstX = Packet.readShort() - (p.getLocation().getRegionX() - 6) * 8;
		int firstY = Packet.readShort() - (p.getLocation().getRegionY() - 6) * 8;
		boolean runSteps = Packet.readByteC() == -1;
		
		p.getWalk().reset();
		p.getWalk().setIsRunning(runSteps);
		p.getWalk().addToWalkingQueue(firstX, firstY);
		for(int i = 0; i < steps; i++)  {
			int localX = Packet.readByte()+firstX;
			int localY = Packet.readByte()+firstY;
			p.getWalk().addToWalkingQueue(localX, localY);
		}
		if (p.getIntermanager().containsTab(16))
			p.getFrames().closeInterface(16);
		if (!p.getIntermanager().containsInterface(8, 137))
			p.getDialogue().finishDialogue();
		if (p.getCombat().hasTarget())
			p.getCombat().removeTarget();
		if (!p.getMask().isTurnToReset())
			p.getMask().setTurnToReset(true);
	}

	/*
	 * Walking
	 */
	@SuppressWarnings("unused")
	private static void PacketId_71(InStream Packet, int Size, Player p) {
		Size -= 14;
		int steps = (Size - 5) / 2;
		int firstY = Packet.readShort() - (p.getLocation().getRegionY() - 6)
				* 8;
		boolean runSteps = Packet.readByteC() == -1;
		int firstX = Packet.readShort() - (p.getLocation().getRegionX() - 6)
				* 8;
		p.getWalk().reset();
		p.getWalk().setIsRunning(runSteps);
		p.getWalk().addToWalkingQueue(firstX, firstY);
		for(int i = 0; i < steps; i++)  {
			int localX = Packet.readByte()+firstX;
			int localY = Packet.readByte()+firstY;
			p.getWalk().addToWalkingQueue(localX, localY);
		}
		if (p.getIntermanager().containsTab(16))
			p.getFrames().closeInterface(16);
		if (!p.getIntermanager().containsInterface(8, 137))
			p.getDialogue().finishDialogue();
		if (p.getCombat().hasTarget())
			p.getCombat().removeTarget();
		if (!p.getMask().isTurnToReset())
			p.getMask().setTurnToReset(true);
	}

	/*
	 * dialogue
	 */
	@SuppressWarnings("unused")
	private static void PacketId_73(InStream Packet, int Size, Player p) {
		int junk = Packet.readShort128();
		int Interface = Packet.readInt();
		short interId = (short) (Interface >> 16);
		byte interChild = (byte) (Interface - (interId << 16));
		p.getDialogue().continueDialogue(interId, interChild);
	}

	/*
	 * Buttons
	 */
	@SuppressWarnings("unused")
	private static void PacketId_79(InStream Packet, int Size, Player p) {
		int interfaceId = Packet.readShort();
		int buttonId = Packet.readShort();
		int buttonId3 = Packet.readShort();
		int buttonId2 = Packet.readShortLE128();
		if (!p.getIntermanager().containsInterface(interfaceId))
			return;
		interfaceScript inter = Scripts
				.invokeInterfaceScript((short) interfaceId);
		if (inter == null) {
			System.out.println("Unhandled Button: " + buttonId
					+ " on interface: " + interfaceId);
			return;
		}
		inter.actionButton(p, 79, buttonId, buttonId2, buttonId3);
	}

	@SuppressWarnings("unused")
	private static void PacketId_82(InStream in, int Size, Player p) {
		int inter1 = in.readInt();
		int firstSlot = in.readShort128();
		int itemUsed = in.readShortLE();
		int secondSlot = in.readShortLE128();
		int usedWith = in.readShort128();
		int inter2 = in.readIntV1();
		Scripts.invokeItemScript((short) itemUsed).thisonitem(p,
				(short) usedWith);
	}

	/*
	 * Chat
	 */
	@SuppressWarnings("unused")
	private static void PacketId_83(InStream Packet, int Size, Player p) {
	}
	
	/*
	 * NPC option attack
	 */
	@SuppressWarnings("unused")
	private static void PacketId_36(InStream in, int size, Player p) {
		boolean bool = (in.readByte128()==1);
		int npcID = in.readShortLE128();
		System.out.println("NPC: Option Attack.");
	}
	
	/*
	 * NPC option talk
	 */
	@SuppressWarnings("unused")
	private static void PacketId_15(InStream in, int size, Player p) {
		boolean bool = (in.readByte()==1);
		int npcID = in.readShortLE();
		System.out.println("NPC: Option Attack.");
	}
	
	/*
	 * NPC option (in the case I dumped at is pickpocket)
	 */
	@SuppressWarnings("unused")
	private static void PacketId_50(InStream in, int size, Player p) {
		boolean bool = (in.readByte()==1);
		int npcID = in.readShort();
	}
	
	/*
	 * NPC option examine
	 */
	@SuppressWarnings("unused")
	private static void PacketId_45(InStream in, int size, Player p) {
		int npcID = in.readShort();
	}
	
	/*
	 * MagicOnNPC / itemonnpc
	 */
	@SuppressWarnings("unused")
	private static void PacketId_28(InStream in, int size, Player p) {
		int eventualItemID = in.readShort128();
		boolean bool = (in.readByte()==1);
		int interfaceHash = in.readIntV1();
		int interfaceID = interfaceHash>>16;
		int childID = interfaceHash-(interfaceID<<16);
		int eventualInterfaceSlot = in.readShort();
		int npcID = in.readShortLE128();
	}

	private void setPackets(Short[] packetsA) {
		for (short packet : packetsA) {
			try {
				packets.put(packet, this.getClass().getDeclaredMethod(
						"PacketId_" + packet, InStream.class, int.class,
						Player.class));
			} catch (SecurityException e) {
				e.printStackTrace();
				System.exit(1);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
				System.exit(1);
			}
		}
	}

	public static Method getPacket(short PacketId) {
		return packets.get(PacketId);
	}

	public static void run(ConnectionHandler p, InStream buffer) {
		synchronized (buffer) {
		while (buffer.remaining() > 0) {
			short opcode = (short)buffer.readUnsignedByte();
			if (opcode < 0 || opcode > 83) {
				System.out.println("Opcode " +	opcode+ " has fake id.");
				break;
			}
			int length = PacketSize[opcode];
			if (length == -1)
				length = buffer.readUnsignedByte();
			else if (length == -2)
				length = buffer.readShort();
			else if (length == -3) {
				length = buffer.remaining();
				System.out.println("Invalid size for Opcode "+opcode+". Size guessed to be "+length);
			}
		     if(length > buffer.remaining()) {
				System.out.println("Opcode " +	opcode+ " has fake size. - expected size " + length);
				break;
		     }else if (!packets.containsKey(opcode))
				System.out.println("Opcode " +opcode+ " not added. - expected size " + length);
		    int startOffset = buffer.offset();
			if (packets.containsKey(opcode)) { //reads packet if contains
				try {
					Method PacketMethod = getPacket(opcode);
					if (PacketMethod != null)
						PacketMethod.invoke(Packets.class, buffer, length, p.getPlayer());
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
			buffer.setOffset(startOffset + length);
		}
		}
	}

}
